package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AlarmMapper;
import com.ruoyi.system.mapper.RecordMapper;
import com.ruoyi.system.domain.Alarm;
import com.ruoyi.system.service.IAlarmService;
import java.math.BigDecimal;

/**
 * 告警信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-28
 */
@Service
public class AlarmServiceImpl implements IAlarmService 
{
    @Autowired
    private AlarmMapper alarmMapper;

    @Autowired
    private RecordMapper recordMapper;

    /**
     * 查询告警信息
     * 
     * @param aId 告警信息主键
     * @return 告警信息
     */
    @Override
    public Alarm selectAlarmByAId(Integer aId)
    {
        return alarmMapper.selectAlarmByAId(aId);
    }

    /**
     * 查询告警信息列表
     * 
     * @param alarm 告警信息
     * @return 告警信息
     */
    @Override
    public List<Alarm> selectAlarmList(Alarm alarm)
    {
        return alarmMapper.selectAlarmList(alarm);
    }

    /**
     * 新增告警信息
     * 
     * @param alarm 告警信息
     * @return 结果
     */
    @Override
    public int insertAlarm(Alarm alarm)
    {
        return alarmMapper.insertAlarm(alarm);
    }

    /**
     * 修改告警信息
     * 
     * @param alarm 告警信息
     * @return 结果
     */
    @Override
    public int updateAlarm(Alarm alarm)
    {
        return alarmMapper.updateAlarm(alarm);
    }

    /**
     * 批量删除告警信息
     * 
     * @param aIds 需要删除的告警信息主键
     * @return 结果
     */
    @Override
    public int deleteAlarmByAIds(Integer[] aIds)
    {
        return alarmMapper.deleteAlarmByAIds(aIds);
    }

    /**
     * 删除告警信息信息
     * 
     * @param aId 告警信息主键
     * @return 结果
     */
    @Override
    public int deleteAlarmByAId(Integer aId)
    {
        return alarmMapper.deleteAlarmByAId(aId);
    }

    @Override
    public List<Map<String, Object>> selectAllStations() {
        return alarmMapper.selectAllStations();
    }

    @Override
    public Map<String, Object> selectAlarmAnalysisByStationId(Integer stationId) {
        Map<String, Object> map = alarmMapper.selectAlarmAnalysisByStationId(stationId);
        // 查询record表，计算工作时长
        BigDecimal totalAlarmDuration = map.get("totalAlarmDuration") == null ? BigDecimal.ZERO : (BigDecimal) map.get("totalAlarmDuration"); // 秒
        int totalAlarmDurationInt = totalAlarmDuration.intValue();

        Object workDurationObj = recordMapper.sumRecordDurationByStationId(stationId);
        int totalWorkDurationInt = 0;
        if (workDurationObj != null) {
            if (workDurationObj instanceof BigDecimal) {
                totalWorkDurationInt = ((BigDecimal) workDurationObj).intValue();
            } else if (workDurationObj instanceof Integer) {
                totalWorkDurationInt = (Integer) workDurationObj;
            } else if (workDurationObj instanceof Long) {
                totalWorkDurationInt = ((Long) workDurationObj).intValue();
            }
        }

        double percent = 0;
        if (totalWorkDurationInt > 0) {
            percent = totalAlarmDurationInt / (totalWorkDurationInt * 60.0);
        }
        map.put("alarmDurationPercent", percent);
         // 类型分布
    List<Map<String, Object>> typeDistribution = alarmMapper.selectTypeDistributionByStationId(stationId);
    map.put("typeDistribution", typeDistribution);

    // 等级分布
    List<Map<String, Object>> levelDistribution = alarmMapper.selectLevelDistributionByStationId(stationId);
    map.put("levelDistribution", levelDistribution);
        return map;
    }

    @Override
public List<Map<String, Object>> selectDailyAlarmCount(Integer stationId, String month) {
    return alarmMapper.selectDailyAlarmCount(stationId, month);
}
@Override
public List<Map<String, Object>> selectDailyAlarmDurationPercent(Integer stationId, String month) {
    return alarmMapper.selectDailyAlarmDurationPercent(stationId, month);
}

}
